<?php

/**
* -------------------------------------------------------------------------------------------------------------------
* @abstract   Handles the CRUD operations for Product model and other methods
* @package    Kiropedic
* @subpackage Controllers
* @author     Darwing Hernández - darwing.her69@gmail.com
* -------------------------------------------------------------------------------------------------------------------
*/

class ProductController extends BaseController
{

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * ATTRIBUTES AND CONSTANTS
  * ---------------------------------------------------------------------------------------------------------------
  */
  public static $restful = true;

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * VIEWS
  * ---------------------------------------------------------------------------------------------------------------
  */

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * CRUD OPERATIONS
  * ---------------------------------------------------------------------------------------------------------------
  */

  public function register()
  {
    $input  = Input::all();
    $index  = array('codctrl');
    $values = array('unique:product_pro,pro_codctrl');

    if (!$this->validate($input, CREATE_PROD, $index, $values))
      return Redirect::to('admin/create/prod/')->withErrors($this->validation)->withInput()->with('notification', 'register_add_warning')->with('opt', 'El producto ');

    $product = Product::create(array(
      'pro_name'        =>  Input::get('nombre'),
      'pro_codctrl'     =>  Input::get('codctrl'),
      'pro_description' =>  Input::get('descrip'),
      'pro_priceunit'   =>  Input::get('precio'),
      'pro_stock'       =>  Input::get('stock'),
      'pro_active'      =>  Input::get('active') == "on"? 1: 0
    ));

    if(Input::get('again') == "on")
      return Redirect::to('admin/create/prod')->with('notification', 'register_add_success')->with('opt', 'El producto ');
    else
      return Redirect::to('admin/list/listprod')->with('notification', 'register_add_success')->with('opt', 'El producto ');
  }

  public function view($id)
  {
    $product = Product::select('pro_code', 'pro_name', 'pro_codctrl', 'pro_description', 'pro_priceunit', 'pro_stock', 'pro_active')->find($id);

    if(!$product) return Redirect::to('admin/list/listprod')->with('notification', 'not_allowed');

    return View::make('admin.product.editProd')->with('product', $product)->with('title', 'Editar Producto')->with('active', 5);
  }

  public function update($id)
  {
    $input  = Input::all();
    $index  = array('codctrl');
    $values = array('unique:product_pro,pro_codctrl,'.$id.',pro_code');

    if (!$this->validate($input, UPDATE_PROD, $index, $values))
      return Redirect::to('admin/viewProd/'.$id)->withErrors($this->validation)->withInput()->with('notification', 'register_edit_warning')->with('opt', 'El producto ');

    $product                  = Product::find($id);
    $product->pro_name        = Input::get('nombre');
    $product->pro_codctrl     = Input::get('codctrl');
    $product->pro_description = Input::get('descrip');
    $product->pro_priceunit   = Input::get('precio');
    $product->pro_stock       = Input::get('stock');
    $product->pro_active      = Input::get('active') == "on"? 1: 0;
    $product->save();

    return Redirect::to('admin/viewProd/'.$id)->with('notification', 'register_edit_success')->with('opt', 'El producto ');
  }

  public static function delete($keys)
  {
    Product::destroy($keys);
    return Redirect::to('admin/list/listprod')->with('notification', 'register_delete_success');
  }

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * OTHER METHODS/ACTIONS AND STUFF
  * ---------------------------------------------------------------------------------------------------------------
  */

}